package com.kyw.android_dev_util.exception;

import com.kyw.android_dev_util.common.MessageConstant;
import com.kyw.android_dev_util.common.ResResult;
import com.kyw.android_dev_util.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public ResResult bizExceptionHandler(HttpServletRequest req, BizException e){
        LogUtil.debug("GlobalExceptionHandler 服务器处理异常:"+e.getErrorMsg());
        return new ResResult(e.getErrorCode(),e.getErrorMsg());
    }
    /**
     * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public ResResult exceptionHandler(HttpServletRequest req, Exception e){
        LogUtil.debug("GlobalExceptionHandler 服务器执行故障:"+e.getMessage());
        return new ResResult(MessageConstant.SERVER_RUN_FAILED,"服务器执行故障");
    }
}